clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees"
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\indiv.dta", clear
ssc install egenmore

/*********************************************************/
/* Reperage initial des BLUE COLLARD individus           */
/* dans les annees 1976, 1982, 1991 et 1999              */
/*********************************************************/
drop csp
/* all workers */
gen all=1
/* blue collar dummy */
gen ouv=(cs2h=="62" | cs2h=="63" | cs2h=="64" | cs2h=="65" | cs2h=="67" | cs2h=="68" | cs2h=="69" )
/* BLUE COLLARD construction */
gen constbc=(nap40=="24" & ouv==1)
/* BLUE COLLARD tradable */
gen trad=((nap40=="01" | nap40=="02" | ///
nap40=="03" | nap40=="04" | nap40=="05" | nap40=="06" | ///
nap40=="07" | nap40=="08" | nap40=="09" | nap40=="10" | ///
nap40=="11" | nap40=="12" | nap40=="13" | nap40=="14" | ///
nap40=="15" | nap40=="16" | nap40=="17" | nap40=="18" | ///
nap40=="19" | nap40=="20" | nap40=="21" | nap40=="22" | ///
nap40=="23" | nap40=="36" | nap40=="37" | nap40=="35" | nap40=="32") & ouv==1)
gen ntrad=((nap40=="24" | nap40=="25" | ///
nap40=="26" | nap40=="27" | nap40=="28" | nap40=="29" | ///
nap40=="30" | nap40=="31" | nap40=="33" | nap40=="34") & ouv==1)
gen bc=(ouv==1)

/****************************************/
/* ouvriers qualitifes et non qualifies */
/****************************************/
gen qual=(cs2_anc=="60" | cs2_anc=="61" ///
| cs2=="62" | cs2=="63"| cs2=="64"| cs2=="65")
/* low skilled BLUE COLLARD */
gen bcnq=bc*(1-qual)
gen constbcnq=constbc*(1-qual)
gen tradnq=trad*(1-qual)
gen ntradnq=ntrad*(1-qual)
/* Skilled BLUE COLLARD */
gen bcq=bc*(qual)
gen constbcq=constbc*(qual)
gen tradq=trad*(qual)
gen ntradq=ntrad*(qual)

/* Employees */
gen emp=(cs2h=="54" | cs2h=="55" | cs2h=="56")
/* Employees in the service sector */
gen empsc=(cs2h=="56")

/* Managers */
gen cad=(cs2h=="23" | cs2h=="31" | cs2h=="33" | cs2h=="34" | cs2h=="35" | cs2h=="37" | cs2h=="38")
/* restricted Managers */
gen cad2=(cs2h=="33" | cs2h=="34" | cs2h=="37" | cs2h=="38")

/* Technicians */
gen tec=(cs2h=="42" | cs2h=="43" | cs2h=="44" | cs2h=="45" | cs2h=="46" | cs2h=="47" )

/* clerk */
gen clrk=(cs2h=="46")

/* emp2 => all the rest */
gen emp2=(bc==0 & cad==0 & tec==0)

drop qual 
/**************************************/
/* 1) => Merge avec les ZE 			  */
/**************************************/
/* elimine la corse */
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* definit les ZE */
joinby cgeo using "M:\ImmPanelRevis17\donnees\cgeo_ze.dta", unm(m)
tab _merge
drop if _merge==1
drop zen

keep all nninouv constbc trad ntrad bc bcnq constbcnq tradnq ntradnq bcq constbcq tradq ntradq ///
 emp empsc cad cad2 tec clrk an emp2 

sort nninouv an
/* gref = reference year of first observation */
rename an gref

save "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\ident_groupmw.dta", replace

clear all
